package com.icloud.system.boot.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.icloud.common.core.api.CommonResult;
import com.icloud.common.web.annotation.AdminOperationLog;
import com.icloud.common.web.annotation.Permission;
import com.icloud.common.web.dto.CommonPageDto;
import com.icloud.system.boot.dto.AdminRoleAllocPermissionDto;
import com.icloud.system.boot.dto.AdminRoleSaveDto;
import com.icloud.system.boot.model.AdminRole;
import com.icloud.system.boot.service.AdminRoleService;
import com.icloud.system.boot.vo.AdminRoleDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController
//@Api(tags = "后台角色管理")
//@RequestMapping("/admin/role")
@RequiredArgsConstructor
public class AdminRoleController {
    private final AdminRoleService adminRoleService;

    //@Permission(value = "system.adminRole.page")
    //@ApiOperation("分页查询角色列表")
    //@GetMapping(value = "/page")
    public CommonResult<IPage<AdminRole>> getList(@ApiParam(name = "name", value = "角色名称") @RequestParam(value = "name", required = false) String name,
                                                  CommonPageDto dto) {
        return CommonResult.success(adminRoleService.getRolePageList(name, dto));
    }

    //@Permission(value = "system.adminRole.add")
    //@AdminOperationLog(value = "账号管理-角色管理-新增角色")
    //@ApiOperation("新增角色")
    //@PostMapping(value = "/add")
    public CommonResult<Void> addRole(@Validated @RequestBody AdminRoleSaveDto dto) {
        adminRoleService.addRole(dto);
        return CommonResult.success();
    }

    //@Permission(value = "system.adminRole.update")
    //@AdminOperationLog(value = "账号管理-角色管理-编辑角色")
    //@ApiOperation("编辑角色")
    //@PutMapping(value = "/{id}")
    public CommonResult<Void> updateRole(@ApiParam("角色id") @PathVariable("id") Long id, @Validated @RequestBody AdminRoleSaveDto dto) {
        adminRoleService.updateRole(id, dto);
        return CommonResult.success();
    }

    //@ApiOperation("查看角色")
    //@GetMapping(value = "/{id}")
    public CommonResult<AdminRoleDetailVo> viewRole(@ApiParam("角色id") @PathVariable("id") Long id) {
        return CommonResult.success(adminRoleService.getRoleDetailById(id));
    }

    //@Permission(value = "system.adminRole.delete")
    //@AdminOperationLog(value = "账号管理-角色管理-删除角色")
    //@ApiOperation("删除角色")
    //@DeleteMapping(value = "/{id}")
    public CommonResult<Void> delRole(@ApiParam("角色id") @PathVariable("id") Long id) {
        adminRoleService.deleteRole(id);
        return CommonResult.success();
    }

    //@ApiOperation("获取所有角色，供给管理员账号分配角色时提供下拉选项")
    //@GetMapping(value = "/all")
    public CommonResult<List<AdminRole>> getAllRole() {
        return CommonResult.success(adminRoleService.getAllRole());
    }

    //@Permission(value = "system.adminRole.distribute")
    //@AdminOperationLog(value = "账号管理-角色管理-给角色分配权限")
    //@ApiOperation("给角色分配权限")
    //@PostMapping(value = "/{id}/permission")
    public CommonResult<Void> allocPermissions(@ApiParam("角色id") @PathVariable(value = "id") Long id, @Validated @RequestBody AdminRoleAllocPermissionDto dto) {
        adminRoleService.allocPermissions(id, dto.getPermissionIds());
        return CommonResult.success();
    }
}
